<?php
namespace Home\Controller;
use Think\Controller;
class BaseController extends Controller {
    public $openid;
    //获取用户的openid
    public function getBaseInfo(){
        $appid='wx3c484176a1ad105d';
        $redirect_uri=urlencode('http://dwc.gametu.com/think/index.php?g=Home&m=player&a=getUserOpenid');
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
        header("location:".$url);
    }

    public function getUserOpenid(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
    }

    public function http_curl($url, $type = 'get', $res = 'json', $arr = '')
    {
        //1.初始化，创建一个新cURL资源
        $ch = curl_init();
        //2.设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//  不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//  不验证证书
        if ($type == 'post') {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
        }
        //3.抓取URL并把它传递给浏览器
        $output = curl_exec($ch);
        if ($res == 'json') {
            return json_decode($output, true);
        }
        //4.关闭cURL资源，并且释放系统资源
        curl_close($ch);
    }
    function script($string,$address,$get){
        if($address){
            if($string){
                echo '<script>alert("'.$string.'");location.href="'.U($address,$get).'"</script>';die;
            }else{
                echo '<script>location.href="'.$address.'"</script>';die;
            }
        }else{
            if($string){
                echo '<script>alert("'.$string.'");history.back();</script>';die;
            }else{
                echo '<script>history.back();</script>';die;
            }
        }
    }
    //生成随机24位订单号
    protected function createOrderId ($length = 24) {
        $seed=md5(microtime());
        $pwd='';
        for($i=0;$i<$length;$i++){
            $pwd.=$seed{mt_rand(0,31)};
        }
        $hash=md5($pwd);
        return substr($hash,0,$length);
    }
    /**
     * @desc 连接DataRedis缓存数据库[5个redis缓存数据库存储用户信息]
     * @param
     * @return
     */
    public function getDataRedis($uid) {
        $mod = C("mod");//redis 个数
        $redis_config = Config::$redis_data_config;

        $redis_name = 'redis'.($uid % $mod);//用户id %  5 求余

        $this->get_redis = new \redis();
        $ret = $this->get_redis->connect($redis_config[$redis_name]['host'],
            $redis_config[$redis_name]['port']);
        if (!$ret) {
            $this->e("redis connect error.");
            $this->returnError(3, "redis connect error.");
        }
        $ret = $this->get_redis->auth($redis_config[$redis_name]['pass']);
        if (!$ret) {
            $this->e("redis auth error.");
            $this->returnError(3, "redis auth error.");
        }
    }

    /**
     * @desc 连接DataRedis缓存数据库[5个redis缓存数据库存储用户信息]
     * @param
     * @return
     */
    public function initDataRedis($uid) {
        $mod = C("mod");//redis 个数
        $redis_config = C("redis_data_config");
        var_dump($redis_config);

        $redis_name = 'redis'.($uid % $mod);//用户id %  5 求余

        $this->data_redis = new \Redis();
        $ret = $this->data_redis->connect($redis_config[$redis_name]['host'],
            $redis_config[$redis_name]['port']);
        if (!$ret) {
            $this->e("redis connect error.");
            $this->returnError(3, "redis connect error.");
        }
        $ret = $this->data_redis->auth($redis_config[$redis_name]['pass']);//验证密码
        if (!$ret) {
            $this->e("redis auth error.");
            $this->returnError(3, "redis auth error.");
        }
    }

    /**
     * @desc 关闭DataRedis缓存
     * @param
     * @return
     */
    public function deinitDataRedis() {
        $this->data_redis->close();
    }

    /**
     * @desc 连接CacheRedis缓存数据库[1个redis缓存数据库存储系统信息]
     * @param
     * @return
     */
    public function initCacheRedis() {
        $redis_config = C("redis_cache_config");
        $this->cache_redis = new \Redis();
        $ret = $this->cache_redis->connect($redis_config['host'], $redis_config['port']);
        if (!$ret) {
            $this->e("redis connect error.");
            $this->returnError(3, "redis connect error.");
        }
        $ret = $this->cache_redis->auth($redis_config['pass']);
        if (!$ret) {
            $this->e("redis auth error.");
            $this->returnError(3, "redis auth error.");
        }
    }

    /**
     * @desc 关闭CacheRedis缓存
     * @param
     * @return
     */
    public function deinitCacheRedis() {
        $this->cache_redis->close();
    }

}